const axios = require('../utils/axios')
const jwt = require('../utils/jwt')

const userModel = require('../model/user/userModel')

class UserController {
  static async login(ctx) {
    // console.log('post参数', ctx.request.body)
    let { code } = ctx.request.body
    const res = await axios({
      url: '/sns/jscode2session',
      method: 'get',
      params: {
        appid: 'wxb4485d746aa3afef',
        secret: 'e9050f653d8e961834f79624b1f378db',
        js_code: code,
        grant_type: 'authorization_code'
      }
    })
    // console.log(res)
    const token = jwt.createToken(res)
    // console.log(token)
    ctx.body = {
      err: 0,
      msg: '成功',
      data: { token }
    }
  }
  static async info(ctx) {
    console.log('user------',ctx.user);
    // console.log('post参数', ctx.request.body)
    let { nickName, avatarUrl, city, country, province } = ctx.request.body
    let ele = {
      nickName,
      avatarUrl,
      city,
      country,
      province
    }

    let doc = await userModel.insertMany([ele])
    ctx.body = {
      err: 0,
      msg: '新增用户成功！',
      data: { data: doc }
    }
  }
}

module.exports = UserController
